package edu.isquen.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

public class Environment implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6535429749200327176L;
	private String encoding;
	private String _contextPath;
	private String _wcpath;
	
	private static Environment ev=null;
	
	private Environment(){
		Properties prop=new Properties();
		InputStream ins=Environment.class.getClassLoader().getResourceAsStream("struts.properties");
		try {
			prop.load(ins);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setEncoding(prop.getProperty("Encoding"));
	}
	
	public static Environment getInstance(){
		if(ev==null){
			ev=new Environment();
		}
		return ev;
	}
	
	public void setApplicationRealPath(String wcpath) {
		_wcpath = wcpath;
	}

	public String getApplicationRealPath() {
		return (_wcpath != null) ? _wcpath : "";
	}
	
	public String getRealPath(String path) {
		String realpath = (path != null) ? getApplicationRealPath() + path : "";
		realpath.replaceAll("\\\\", "/");

		return realpath;
	}

	public String getContext(String uri) {
		if (_contextPath.equals("/")) {
			return uri;
		} else {
			return _contextPath + uri;
		}
	}
	
	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public String get_contextPath() {
		return _contextPath;
	}

	public void set_contextPath(String path) {
		_contextPath = path;
	}

	public String get_wcpath() {
		return _wcpath;
	}

	public void set_wcpath(String _wcpath) {
		this._wcpath = _wcpath;
	}
}
